/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaPopup.java
 *
 * Created on 13/12/2013, 21:03:20
 */
package views;

import config.LogErro;
import controllers.DimensaoController;
import controllers.ProcedimentoController;
import controllers.TabAuxController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import models.entidades.Dimensao;
import models.entidades.Procedimento;
import models.entidades.TabAux;

/**
 *
 * @author Wenderson
 */
public class TelaPopup extends javax.swing.JDialog {

    enum Captura { TAB_AUX, DIMENSAO, PROCEDIMENTO };
    
    private String retorno = "";
    private List lista;
    
    
    /** Creates new form TelaPopup */
    public TelaPopup(java.awt.Frame parent, boolean modal, Captura captura) {
        super(parent, modal);
        initComponents();
        
        this.setLocationRelativeTo(null);
        
        this.carregaLista(captura);
    }
    
    private void carregaLista(Captura captura) {
        try {
            
        if (captura == Captura.PROCEDIMENTO) {
            lista = ProcedimentoController.obterTodos();
            this.jl_titulo.setText("Escolha o Procedimento");
        } else if (captura == Captura.DIMENSAO) {
            lista = DimensaoController.obterTodos();
            this.jl_titulo.setText("Escolha a Dimensao");
        } else {
            lista = TabAuxController.obterTodos();
            this.jl_titulo.setText("Escolha a Tabela Auxiliar");
        }
        
        this.preencheLista(lista, captura);
        
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
            LogErro.gravaErro(e);
        }
    }            
    
    private void preencheLista(List lista, Captura captura) {
        int id = 0;
        String nome = "";
        String esquema = "";
        
        int linha = 0;
        
        DefaultTableModel model = (DefaultTableModel) this.jt_descricao.getModel();
        
        for (Iterator it = lista.iterator(); it.hasNext();) {
            
            switch (captura) {
                case PROCEDIMENTO: 
                    Procedimento p = (Procedimento) it.next();
                    id = p.getCdProcedimento();
                    nome = p.getNome();
                    esquema = p.getEsquema();
                    break;
                case DIMENSAO:
                    Dimensao d = (Dimensao) it.next();
                    id = d.getCdDimensao();
                    nome = d.getNome();
                    esquema = d.getEsquema();
                    break;
                case TAB_AUX:
                    TabAux t = (TabAux) it.next();
                    id = t.getCdTabAux();
                    nome = t.getNome();
                    esquema = t.getEsquema();
                    break;
            }
            //this.jt_descricao.setValueAt(id, linha, 0);
            //this.jt_descricao.setValueAt(esquema, linha, 1);
            //this.jt_descricao.setValueAt(nome, linha, 2);
            model.addRow(new Object[] {id, esquema, nome});
            
            linha++;
        }
    }
    
    public String getRetorno() {
        return this.retorno;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jl_titulo = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jt_descricao = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jl_titulo.setText("jLabel1");

        jt_descricao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Esquema", "Nome"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jt_descricao.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jt_descricaoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jt_descricao);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 515, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jl_titulo)
                .addGap(282, 282, 282))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(jl_titulo)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(34, Short.MAX_VALUE))
        );

        getAccessibleContext().setAccessibleParent(this);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jt_descricaoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jt_descricaoMouseClicked
        if (evt.getClickCount() == 2) {
            int row = this.jt_descricao.getSelectedRow();
            String id = this.jt_descricao.getValueAt(row, 0).toString();
            String esquema = this.jt_descricao.getValueAt(row, 1).toString();
            String nome = this.jt_descricao.getValueAt(row, 2).toString();
            
            this.retorno = id + ";" + esquema + ";" + nome;
            this.dispose();
        }
    }//GEN-LAST:event_jt_descricaoMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaPopup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaPopup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaPopup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaPopup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaPopup dialog = new TelaPopup(new javax.swing.JFrame(), true, Captura.DIMENSAO);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jl_titulo;
    private javax.swing.JTable jt_descricao;
    // End of variables declaration//GEN-END:variables
}
